package com.example.demo.Controller;

import com.example.demo.Entity.transfer.MedicalrecordsDTO;
import com.example.demo.Service.MedicalrecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.Random;


/**
 * @author quito
 * @date 2019/04/30
 * */
@RestController
@RequestMapping("/medicalrecords")
public class MedicalrecordsController {


    @Autowired
    private MedicalrecordsService medicalrecordsService;

    /**
     * 根据手机号查询病例
     * */
    @GetMapping(value = "/list/phone/{phone}")
    public  Map<String, Object> findMedicalrecordsByPhone(@PathVariable("phone") String phone){
        return medicalrecordsService.QueryMedicalcareByphone(phone);
    }


    /**
     * 根据手机号查询病例
     * */
    @GetMapping(value = "/list")
    public  Map<String, Object> findAllMedicalrecordsByPhone(HttpServletRequest request, HttpServletResponse response){
        response.addHeader("Access-Control-Allow-Origin", "*");
        return medicalrecordsService.QueryAllMedicalcareByphone();
    }



    /**
     * 根据USERID查询病例
     * */
    @GetMapping(value = "list/{userId}")
    public Map<String, Object> findMedicalcareByUserId(@PathVariable("userId") int user_id){
        return medicalrecordsService.QueryMedicalcareByUserId(user_id);
    }

    /**
     * 根据ID查询病例
     * */
    @GetMapping(value = "/{id}")
    public Map<String, Object> QueryMedicalcareById(@PathVariable("id") int id,HttpServletRequest request, HttpServletResponse response){
        response.addHeader("Access-Control-Allow-Origin", "*");
        System.err.println(":" + id);
        Map<String, Object> map = medicalrecordsService.QueryMedicalcareById(id);
        System.err.println(map);
        return map;
    }

    /**
     * 删除病例
     * */
    @DeleteMapping(value = "/{id}")
    public Map<String, Object> DelMedicalcareById(@PathVariable("id") int id){
        return medicalrecordsService.DelMedicalcareById(id);
    }

    /**
     * 新增病例
     * */
    @PostMapping()
    public Map<String, Object> addMedicalcareById(HttpServletRequest request, HttpServletResponse response){
        response.addHeader("Access-Control-Allow-Origin", "*");
        MedicalrecordsDTO medicalrecordsDTO = new MedicalrecordsDTO();
        Random random = new Random();
        medicalrecordsDTO.setReportthephysician(request.getParameter("reportthephysician"));
        medicalrecordsDTO.setName(request.getParameter("name"));
        medicalrecordsDTO.setPhone(request.getParameter("phone"));
        medicalrecordsDTO.setSex(request.getParameter("sex"));
        medicalrecordsDTO.setAge(request.getParameter("age"));
        medicalrecordsDTO.setPathologicaltype(request.getParameter("pathologicaltype"));
        medicalrecordsDTO.setConclusion(request.getParameter("conclusion"));
        medicalrecordsDTO.setCordbase64(request.getParameter("cordbase64"));
        medicalrecordsDTO.setUser_id(random.nextInt(9999999));
        medicalrecordsDTO.setAddress(request.getParameter("address"));
        medicalrecordsDTO.setDiagnosis(request.getParameter("conclusion"));
        medicalrecordsDTO.setIdcard("4209"+random.nextInt(9999999)+random.nextInt(9999999)+random.nextInt(9999999));
        medicalrecordsDTO.setReportingunit("XXX市人民医院");
        return medicalrecordsService.addMedicalcareById(medicalrecordsDTO);
    }

    /**
     * 修改病例
     * */
    @PostMapping("/update")
    public Map<String,Object>  update(HttpServletRequest request, HttpServletResponse response){
        response.addHeader("Access-Control-Allow-Origin", "*");
        System.err.println("===="+request.getParameter("cordbase64"));
        MedicalrecordsDTO medicalrecordsDTO = new MedicalrecordsDTO();
        Random random = new Random();
        medicalrecordsDTO.setId(Integer.valueOf(request.getParameter("id")));
        medicalrecordsDTO.setReportthephysician(request.getParameter("reportthephysician"));
        medicalrecordsDTO.setName(request.getParameter("name"));
        medicalrecordsDTO.setPhone(request.getParameter("phone"));
        medicalrecordsDTO.setSex(request.getParameter("sex"));
        medicalrecordsDTO.setAge(request.getParameter("age"));
        medicalrecordsDTO.setPathologicaltype(request.getParameter("pathologicaltype"));
        medicalrecordsDTO.setConclusion(request.getParameter("conclusion"));
        medicalrecordsDTO.setCordbase64(request.getParameter("cordbase64"));
        medicalrecordsDTO.setAddress(request.getParameter("address"));
        medicalrecordsDTO.setDiagnosis(request.getParameter("conclusion"));
        return medicalrecordsService.update(medicalrecordsDTO);
    }

    /**
     * 医嘱下载
     * */
    @GetMapping("/download/{id}")
    public Map<String,Object>  download(@PathVariable("id")  int id,HttpServletRequest request, HttpServletResponse response){

        response.addHeader("Access-Control-Allow-Origin", "*");
        return medicalrecordsService.download(id);
    }


}
